<template>
    <div>
        {{days}}
        <span @click="sign">{{isSign?'已经签到':'未签到'}}</span>
    </div>
</template>
<script>
import axios from 'axios'
export default {
    data(){
        return {
            isSign:false,
            phone:localStorage.getItem('phone'),
            days:0
        }
    },
    created(){
        axios.get('/api/isSign',{params:{phone:this.phone}}).then(res => {
            console.log(res);
            if(res.data.code === 1){
                //已经签到
                this.isSign = true;
            }else{
                this.isSign = false;
            }
        })
        this.getDay();

    },
    methods:{
        sign(){
            if(this.isSign){
                alert('今日已签到，不能重复签到')
            }else{
                axios.post('/api/sign',{phone:this.phone}).then(res => {
                    if(res.data.code === 1){
                        alert('签到成功');
                        this.isSign = true;
                        this.getDay()
                    }
                })
            }
        },
        getDay(){
            axios.get('/api/days',{params:{phone:this.phone}}).then(res => {
                console.log(res);
                if(res.data.code === 1){
                    this.days = res.data.count;
                }
            })
        }
    }
}
</script>